package com.tc.netty.three;


import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Random;

/**
 * @Auther: tianchao
 * @Date: 2022/2/14 09:18
 * @Description: 粘包半包定长解决方案
 */
@Slf4j
public class Client2 {

    public static void main(String[] args) {
//        fill10Bytes('1',5);
//        fill10Bytes('2',2);
//        fill10Bytes('3',10);
        send();
    }
    public static byte[] fill10Bytes(char c ,int len){
        final byte[] bytes = new byte[10];
        Arrays.fill(bytes,(byte)'_');
        for (int i = 0; i < len; i++) {
            bytes[i] = (byte) c;
        }
        System.out.println(new String(bytes));
        return bytes;
    }

    private static void send(){
        NioEventLoopGroup worker = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.group(worker);
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                    ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){

                        @Override
                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                            ByteBuf buffer = ctx.alloc().buffer();
                            char c = '0';
                            final Random r = new Random();
                            //生成10个定长的字节数组全部写入到一个ByteBuf中，最后发送出去
                            //目的：检验服务器能否每次取到定长ByteBuf
                            for (int i = 0; i < 10; i++) {
                                final byte[] bytes = fill10Bytes(c, r.nextInt(10) + 1);
                                buffer.writeBytes(bytes);
                                c++;
                            }
                            ctx.channel().writeAndFlush(buffer);
                        }
                    });
                }
            });
            final ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8080).sync();
            channelFuture.channel().closeFuture().sync();
        }catch (InterruptedException e){
            log.error("client error {}",e);
        }finally {
            worker.shutdownGracefully();
            log.debug("final 释放资源");
        }
    }
}
